package com.easyadmin.controller;

import com.easyadmin.annotation.GlobalInterceptor;
import com.easyadmin.annotation.VerifyParam;
import com.easyadmin.entity.enums.OperModuleEnum;
import com.easyadmin.entity.enums.PermissionCodeEnum;
import com.easyadmin.entity.po.SysRole;
import com.easyadmin.entity.query.SysRoleQuery;
import com.easyadmin.entity.vo.AjaxResponseVO;
import com.easyadmin.entity.vo.PaginationResult;
import com.easyadmin.exception.BusinessException;
import com.easyadmin.service.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/settings")
public class SysRoleController extends ABaseController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysRoleService sysRoleService;

    /**
     * @Description: 分页获取角色信息
     * @auther: 多多洛
     * @date: 10:29 2020/11/23
     * @param: [roleQuery]
     * @return: AjaxResponseVO
     */
    @RequestMapping("loadRoles")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ROLE)
    public AjaxResponseVO loadRoles(SysRoleQuery roleQuery) {
        try {
            roleQuery.setOrderBy("create_time desc");
            PaginationResult<SysRole> roles = sysRoleService.findListByPage(roleQuery);
            return getSuccessResponseVO(roles);
        } catch (Exception e) {
            logger.error("获取角色信息异常", e);
            return getServerErrorResponseVO();
        }
    }


    /**
     * @Description: 获取所有角色
     * @auther: 多多洛
     * @date: 10:29 2020/11/23
     * @param: [roleQuery]
     * @return: AjaxResponseVO
     */
    @RequestMapping("loadAllRoles")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ROLE)
    public AjaxResponseVO loadAllRoles(SysRoleQuery roleQuery) {
        try {
            roleQuery.setOrderBy("create_time desc");
            List<SysRole> roles = sysRoleService.findListByParam(roleQuery);
            return getSuccessResponseVO(roles);
        } catch (Exception e) {
            logger.error("获取角色信息异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 获取角色详情
     * @auther: 多多洛
     * @date: 10:29 2020/11/23
     * @param: [roleId]
     * @return: AjaxResponseVO
     */
    @RequestMapping("getRoleByRoleId")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ROLE)
    public AjaxResponseVO getRoleByRoleId(Integer roleId) {
        try {
            SysRole sysRole = sysRoleService.getSysRoleByRoleId(roleId);
            return getSuccessResponseVO(sysRole);
        } catch (Exception e) {
            logger.error("通过角色ID获取角色信息异常", e);
            return getServerErrorResponseVO();
        }
    }


    /**
     * @Description: 新增、修改 角色
     * @auther: 多多洛
     * @date: 10:30 2020/11/23
     * @param: [role, menuIds]
     * @return: AjaxResponseVO
     */
    @RequestMapping("saveRole")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ROLE_EDIT, opObject = OperModuleEnum.OBJECT_ROLE, opDescription = "保存角色,角色信息:#{#param0}," +
            "菜单ID:#{#param1}")
    public AjaxResponseVO saveRole(@VerifyParam SysRole role, @VerifyParam(required = true) String menuIds) {
        try {
            sysRoleService.saveRole(role, menuIds);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("保存角色异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("保存角色异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 保存角色菜单
     * @auther: 多多洛
     * @date: 10:30 2020/11/23
     * @param: [roleId, menuIds]
     * @return: AjaxResponseVO
     */
    @RequestMapping("saveRoleMenu")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ROLE_EDIT, opObject = OperModuleEnum.OBJECT_ROLE, opDescription = "保存角色菜单,角色ID:#{#param0}," +
            "菜单ID:#{#param1}")
    public AjaxResponseVO saveRole(@VerifyParam(required = true) Integer roleId, @VerifyParam(required = true) String menuIds) {
        try {
            sysRoleService.saveRoleMenu(roleId, menuIds);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("保存角色菜单异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("保存角色菜单异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 删除角色
     * @auther: 多多洛
     * @date: 10:30 2020/11/23
     * @param: [roleId]
     * @return: AjaxResponseVO
     */
    @RequestMapping("/delRole")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ROLE_DEL, opObject = OperModuleEnum.OBJECT_ROLE, opDescription = "删除角色，角色id:#{#param0}")
    public AjaxResponseVO roleDel(@VerifyParam(required = true) Integer roleId) {
        try {
            sysRoleService.deleteByRoleId(roleId);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("删除角色异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("删除角色异常", e);
            return getServerErrorResponseVO();
        }
    }
}
